<template>
  <div class="clearfix table-out">
    <div class="clearfix out-con">
      <el-button
        type="primary"
        plain
        size="small"
        class="return pointer"
        @click="returnGo"
        ><i class="iconfont icon-fanhui"></i> 返回
      </el-button>
      <top-step ref="topStepRef"></top-step>
      <div class="clear h20"></div>
      <div class="fxl left-main clearfix">
        <form-data ref="formDataRef" @getPdf="getPdf"></form-data>
      </div>
      <div class="fxr right-main clearfix">
        <div class="clearfix top_line">复核</div>

        <div class="pw-list clearfix">
          <div class="icon clearfix">
            <b class="one">评委</b>
            <b class="two">评分</b>
            <b class="three" style="text-align: center">评语</b>
          </div>
          <div class="icon clearfix" v-for="(item, idx) in pingwlb" :key="idx">
            <div class="one">{{ idx + 1 }}</div>
            <div class="two">
              <b style="color: #ff0000">{{ item.pingf }}</b>
            </div>
            <div class="three">{{ item.pingy }}</div>
          </div>
          <div class="icon clearfix" style="padding: 3px 15px">
            <b
              >平均分：<span style="color: #ff0000">{{ sum }}</span> 分</b
            >
          </div>
        </div>
        <div class="clarfix h10"></div>
        <div class="clearfix">
          <el-input class="font-red" type="number" v-model="fuhf" placeholder="请输入复核分"></el-input>
        </div>
        <div class="clarfix h10"></div>
        <el-select
          v-model="saisjc"
          placeholder="请选择赛事进程"
          @change="saisjcOnChange"
        >
          <el-option
            v-for="item in saisjclb"
            :key="item.id"
            :label="item.jincmc"
            :value="item.id"
          >
          </el-option>
        </el-select>
        <div class="clarfix h10"></div>
        <el-select
          v-model="shenhjg"
          placeholder="请选择复核结果"
          :disabled="saisjc == '' || saisjc == null"
        >
          <el-option
            v-for="item in options"
            :key="item.itemValue"
            :label="item.itemText"
            :value="item.itemValue"
          >
          </el-option>
        </el-select>
        <div class="clear h10"></div>
        <div class="but-out clearfix">
          <el-button type="primary" class="submit-big" @click="submit"
            >提交</el-button
          >
        </div>
      </div>
      <div class="clear h20"></div>
      <form-data-pdf
        v-show="pdfStatic"
        :pdfTitle="pdfTitle"
        :pdf-title-sub="pdfTitleSub"
        :form-data="pdfData"
        ref="formDataPdfRef"
      ></form-data-pdf>
    </div>
  </div>
</template>

<script>
import axios from "axios";
import pubJs from "./pub";
export default {
  mixins: [pubJs],
  data() {
    return {
      saisjc: null,
      saisjclb: [],
      fuhf: null,
      shenhjg: null,
      options: [],
      id: null,
      pingwlb: [],
      pingy: null,
      zongf: 0.0,
      guizfs: {},
      pingsgz: [],
      formData2: {},
      canszpId: null,
      formData: {},
      formDefine: [],
      daqsc: null,
      saisId: null,

      sumStatic:true,
      topData: [],
      menuSelect: 0,
      activeName: 0,
      sum: 0,

    };
  },

  inject: ["imageUrl", "serviceUrl"],

  mounted() {
    this.saisqyId = this.$route.query.saisqyId;
    this.saisjcId = this.$route.query.saisjcId;
    this.canszpId = this.$route.query.id;
    this.saisId = this.$route.query.saisId;
    // this.fuhf = this.$route.query.fuhf;

    this.$refs["topStepRef"].getList(
      {
        saisId: this.saisId,
      },
      "pings"
    );
    this.$refs["formDataRef"].getFormDefine(
      {
        saisId: this.saisId,
        cansrsf: this.userInfo.cansrsf,
      },
      "pings"
    );
    this.$refs["formDataRef"].getFormData(
      {
        saisId: this.saisId,
        canszpId: this.canszpId,
      },
      "pings"
    );

    let that = this;

    axios
      .get(that.serviceUrl + "/saisjc2/cdba2Saisjc/list", {
        headers: {
          "X-Access-Token": localStorage.getItem("token"),
        },
        params: {
          saisId: that.saisId,
          _t: new Date().getTime(),
        },
      })
      .then((resp) => {
        that.saisjclb = resp.data.result.records;
      })
      .catch((error) => {
        //console.log(error);
      });

    this.sum = 0;

    // 评审记录
    axios
      .get(that.serviceUrl + "/saisjczd2/cbda2Saisjczd/getPingwlb4Fuh", {
        headers: {
          "X-Access-Token": localStorage.getItem("token"),
        },
        params: {
          zuopId: that.canszpId,
          saisjcId: that.saisjcId,
          quyId: that.saisqyId,
          saisId: that.saisId,
          _t: new Date().getTime(),
        },
      })
      .then((resp) => {
        that.pingwlb = resp.data.result;

        if(that.pingwlb.length>3){
          let sumAll = []
          that.pingwlb.forEach(item=>{
            sumAll.push(item.pingf)
          })
          let sortedScores = sumAll.sort((a, b) => a - b);
          let highest = sortedScores.pop(); // 去掉最高分
          let lowest = sortedScores.shift(); // 去掉最低分
          let adjustedTotal = sortedScores.reduce((sum, score) => sum + score, 0);
          this.sum =  (adjustedTotal / (sortedScores.length)).toFixed(2); // 保留两位小数
        }else{
          let i =0,count=0
          that.pingwlb.forEach(item=>{
            if(item.pingf/1>0){
              i = i+1
              count += item.pingf/1
            }
          })
          if(count/i>0){
            this.sumStatic = true
            this.sum = (count/i).toFixed(2)
          }else{
            this.sumStatic = true
            this.sum = 0
          }
        }
        console.log(this.pingwlb,'******',this.sum,sumAll,sortedScores);
      })
      .catch((error) => {
        //console.log(error);
      });
  },
  computed: {
    averageScore() {

    }
  },
  methods: {
    averageScore(data) {
      let sortedScores = data.sort((a, b) => a - b);
      let highest = sortedScores.pop(); // 去掉最高分
      let lowest = sortedScores.shift(); // 去掉最低分
      let adjustedTotal = sortedScores.reduce((sum, score) => sum + score, 0);
      return (adjustedTotal / (sortedScores.length)).toFixed(2); // 保留两位小数
    },

    saisjcOnChange(e) {
      console.log(e);
      let that = this;
      this.shenhjg = "";

      // 动态字典
      // 分站赛
      let _dictId = "1781673229923397633";

      // 半决赛
      if (e == "1779359204496863233") _dictId = "1784005959839469569";
      // 总决赛
      if (e == "1779359389557944322") _dictId = "1784075566897770498";

      axios
        .get(that.serviceUrl + "/sys/dictItem/list", {
          headers: {
            "X-Access-Token": localStorage.getItem("token"),
          },
          params: {
            dictId: _dictId,
            _t: new Date().getTime(),
          },
        })
        .then((resp) => {
          that.options = resp.data.result.records;
        })
        .catch((error) => {
          //console.log(error);
        });
    },

    submit() {
      console.info("kkkkkkkk");
      if (this.saisjc == "" || this.saisjc == null) {
        this.$message.warning("请选择赛事进程");
        return;
      }
      if (this.shenhjg == "" || this.shenhjg == null) {
        this.$message.warning("请选择复核结果");
        return;
      }
      let that = this;
      const config = {
        headers: {
          "X-Access-Token": localStorage.getItem("token"),
        },
      };

      let data = {
        pingjf: that.sum,
        saisqyId: that.saisqyId,
        fuhf: that.fuhf,
        saisjcId: that.saisjc,
        saisId: that.saisId,
        canszpId: that.canszpId,
        pingszt: that.shenhjg,
      };

      axios
        .put(that.serviceUrl + "/fuh2/cbda2Fuh/fuh", data, config)
        .then((resp) => {
          console.log(resp);
          if (resp.data.success) {
            that.$message.success("成功");
            this.$router.go(-1); // 返回上一个页面
            // this.$router.push("/pc/review");
          } else {
            that.$message.error(resp.data.message);
          }

        })
    },

    menuChange(item, index) {
      this.menuSelect = index;
      console.log(item, index);
    },

    returnGo() {
      this.$router.go(-1); // 返回上一个页面
    },
  },
};
</script>

<style scoped lang="less">
.left-main {
  width: 70%;
}
.right-main {
  width: calc(30% - 70px);
  background: #fff;
  box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
  border-radius: 10px;
  padding: 20px;
}
.pf-list {
  .line {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    margin: 0px 0px 10px 0px;
  }
  .num-font {
    width: 120px;
    font-weight: bold;
  }
  .num-inp {
    width: 100px;
  }
  .intro {
    width: 100%;
    display: block;
    opacity: 0.8;
    margin: 0px 0px 20px 0px;
  }
}
.top_line {
  font-size: 18px;
  font-weight: bold;
  border-bottom: 1px dashed #ccc;
  padding: 0px 0px 5px 0px;
  margin: 0px 0px 10px 0px;
}
.pw-list {
  background: rgba(249, 249, 249, 0.4);
  border: 1px solid #eeeded;

  margin: 0px 0px 8px 0px;
  border-radius: 5px;
  .icon {
    padding: 5px 0px;
    line-height: 22px;
    border-bottom: 1px dashed #eeeded;
    display: flex;
    align-items: center;
    .one {
      width: 60px;
      text-align: center;
    }
    .two {
      text-align: center;
      width: 80px;
    }
    .three {
      width: calc(100% - 160px);
    }
  }
}
.font-red :deep(.el-input__inner){
  color:#ff0000;
  font-weight: bold;
}
</style>
